<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>rossdomain | FIM (fragment identifier messaging)</title>
	<meta http-equiv="X-UA-Compatible" content="IE=Edge">
	<style>
	html{
		font-family: Verdana, Arial;
		color: #333;
		background:#EEE;
	}
	#info{
		color:#00F;
	}
	</style>
</head>
<body>
	<p>iframe</p>
	<button id='trigger'>Send Message</button>
	<p id='info'></p>
	<script>
	(function(){
		
		//UA detect
		var ua = navigator.userAgent,
			  av = navigator.appVersion,
			  ie = (document.all && ua.indexOf('Opera')==-1);
		  if(ie){
			 ie = parseFloat(av.split("MSIE ")[1]) || undefined;
			 var mode = document.documentMode;
			  if(mode && mode != 5 && Math.floor(ie) != mode){
				ie = mode;
			  }
		  };

		var cache_bust = 1,
			interval_id,
			last_hash,
			addEvent = function(type, handle, el){
				  addEvent = el.addEventListener ? function(type, handle, el){
						el.addEventListener(type, handle, false);
					}:function(type, handle, el){
						el.attachEvent("on" + type, handle);
					};
				   addEvent(type ,handle, el);
			},
			removeEvent = function(type, handle, el){
				  removeEvent = el.removeEventListener ? function(type, handle, el){
						el.removeEventListener(type, handle);
					}:function(type, handle, el){
						el.detachEvent("on" + type, handle);
					};
				   removeEvent(type ,handle, el);
			},
			sendMessage = function(message, win_location, win){
				win.location = win_location.replace( /#.*$/, '' ) + '#' + (+new Date) + (cache_bust++) + '&' + message;
			},
			receiveMessage = function(callback, delay){
				var updateHash = function(){
					var hash = document.location.hash,
						 re = /^#?\d+&/;
					  if ( hash !== last_hash && re.test( hash ) ) {
						last_hash = hash;
						callback({ data: hash.replace( re, '' ) });
					  }
				}

				 if('onhashchange' in window && 
					(!ie || (ie >= 8 && document.compatMode != "BackCompat"))){
						addEvent('hashchange', updateHash , window);
						return;
				 }
	
				interval_id && clearInterval( interval_id );
				interval_id = setInterval(updateHash, (delay||100));
			},
			info = document.getElementById('info');

		receiveMessage(function(event){
			info.innerHTML = event.data;
		}, window);

		addEvent('click', function(){
			sendMessage('iframe send a message at ' + new Date(), 'http://www.a.com/crossdomain/fragment-identitier-messaging/host.html', parent);
		}, document.getElementById('trigger'));
		
			
	})();
	</script>
</body>
</html>
